﻿using LD.Host;
using ReaLTaiizor.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YTE.Control;
using 右下角弹窗.DiagShow;

namespace LD.BC
{
    public partial class FrmAxisPointEdit1 : FrmOKCancel
    {
        private bool _edit = false;
        public Action<TB_Point> ActionPoint;
        public FrmAxisPointEdit1()
        {
            InitializeComponent();
            this.Shown += FrmBcSettingPointDialog_Shown;
            this.btn_ok.Click += Btn_ok_Click;
            Init();
        }
        public FrmAxisPointEdit1(TB_Point point) : this()
        {
            if (point != null)
            {
                foreach (var item in point.AxisPointDic)
                {
                    var check = this.panel2.Controls.Find(item.Value.AxisId + "", false).FirstOrDefault();
                    if (check != null)
                        (check as HopeCheckBox).Checked = true;
                }
                _edit = true;
            }
        }
        private void Btn_ok_Click(object sender, EventArgs e)
        {
            Dialog dialog = new Dialog();
            TB_Point point = new TB_Point();
            Dictionary<string, TB_Point_Axis> AxisDic = new Dictionary<string, TB_Point_Axis>();
            string name = this.txt_name.Text.Trim();
            try
            {
                foreach (var item in this.panel2.Controls)
                {
                    if (typeof(HopeCheckBox) == item.GetType())
                    {
                        var check = item as HopeCheckBox;
                        if (check.Checked)
                            AxisDic.Add(check.Text, new TB_Point_Axis() { AxisId = Convert.ToUInt16(check.Name), AxisName = check.Text });
                    }
                }
                point.Name = name;
                point.AxisPointDic = AxisDic;

                ActionPoint?.Invoke(point);

                this.txt_name.Text = string.Empty;

                if (_edit)
                    this.Close();
                dialog.Success("保存成功");
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void FrmBcSettingPointDialog_Shown(object sender, EventArgs e)
        {
        }
        private void Init()
        {
            int y = 8;
            foreach (var item in BcFactory.Instance.Config.AxisParam)
            {
                var hope = GetAxis(item.Name, item.AxisId);
                hope.Location = new System.Drawing.Point(82, y);
                this.panel2.Controls.Add(hope);
                y += 30;
            }
        }
        private HopeCheckBox GetAxis(string axisName, int axisId)
        {
            HopeCheckBox check = new HopeCheckBox();
            check.Name = axisId + "";
            check.Text = axisName;
            return check;
        }
    }
}
